package com.qifenqian.turbot.widget

import android.content.Context
import android.support.annotation.Dimension.SP
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.Gravity
import android.widget.Button
import android.widget.EditText
import com.qifenqian.turbot.R
import com.qifenqian.turbot.util.Lcat

/**
 * @author H
 * @createDate 2018/6/28 12:27
 * @reference
 * @updateDate
 * @description 根据EditText是否有文本，来确定Button是否可以点击
 * @detail 文字16sp，白色文字，高度48dp
 * @since
 */
class RectangleButton @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = android.R.attr.button) : Button(context, attrs, defStyleAttr) {

    init {
        gravity = Gravity.CENTER
        val ta = context.obtainStyledAttributes(attrs!!, R.styleable.RectangleButton)
        val drawable = ta.getDrawable(R.styleable.RectangleButton_background_drawable)
        val textColor = ta.getColor(R.styleable.RectangleButton_textColor, context.resources.getColor(R.color.white))
        val textSize = ta.getInt(R.styleable.RectangleButton_textSize, 16).toFloat()

        setTextColor(textColor)
        setTextSize(SP, textSize)
        if (drawable != null) {
            background = drawable
        } else {
            setBackgroundResource(R.color.gray_666666)
        }
        ta.recycle()
    }

    fun setButtonEnable(et: EditText) {
        isEnabled = false
        et.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}

            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}

            override fun afterTextChanged(editable: Editable) {
                isEnabled = !TextUtils.isEmpty(editable.toString())
            }
        })
    }

    fun setButtonEnable(et1: EditText, et2: EditText) {
        isEnabled = false
        et1.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}

            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}

            override fun afterTextChanged(editable: Editable) {
                isEnabled = if (!TextUtils.isEmpty(editable.toString())) {
                    !TextUtils.isEmpty(et2.text.toString())
                } else {
                    false
                }
                Lcat.a("isEnabled", isEnabled);
            }
        })
        et2.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}

            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}

            override fun afterTextChanged(editable: Editable) {
                isEnabled = if (!TextUtils.isEmpty(editable.toString())) {
                    !TextUtils.isEmpty(et1.text.toString())
                } else {
                    false
                }
                Lcat.a("isEnabled", isEnabled);
            }
        })
    }

}
